package com.kang.gaklibrary.controller;

import com.kang.gaklibrary.model.GoodsRecord;
import com.kang.gaklibrary.model.User;
import com.kang.gaklibrary.service.GoodsRecordService;
import com.kang.gaklibrary.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/userIndex")
public class UserIndexController {
    @Autowired
    private UserService userService;
    @Autowired
    private GoodsRecordService goodsRecordService;  // 已购实体服务类

    // 检查账户登录
    public User check(HttpServletRequest request){
        User user = null;
        HttpSession session = request.getSession(false);
        if (session != null){
            user = (User) session.getAttribute("user");
        }
        return user;
    }

    @RequestMapping("/information")
    public String getInformation(HttpServletRequest request){
        User user = check(request);
        if (user != null){
            return "userIndex/userInformation";
        }else {
            return "login";
        }
    }


    @PostMapping("/changeInformation")
    @ResponseBody
    public int changeInformation(@RequestParam("key") String key,@RequestParam("value") String value
    , HttpServletRequest request){
        User user = check(request);
        if(user != null){
            System.out.println("用户："+ user.getUserId() + "更改了 " + key + " 信息");
            int flag = userService.updateSelectiveByKey(user.getUserId(), key, value);
            User nowUser = userService.userLogin(user.getUserId());
            request.getSession(false).setAttribute("user", nowUser);
            return flag;
        }else {
            return 0;
        }
    }


    @RequestMapping("/accountInformation")
    public String accountInformation(HttpServletRequest request){
        User user = check(request);
        if (user != null){
            return "userIndex/accountInformation";
        }else {
            return "login";
        }
    }


    @RequestMapping("/shoppingCart")
    public String shoppingCart(HttpServletRequest request, Model model){
        User user = check(request);
        if(user != null){
            List<GoodsRecord> goodsRecords = goodsRecordService.selectBookGoodsByUserId(user.getUserId());
            model.addAttribute("goodsBookRecordList", goodsRecords);
            if (goodsRecords == null){
                System.out.println("...........................................................................................");
            }
        }
        return "userIndex/shoppingCart";
    }


    @RequestMapping("/boughtGoods")
    public String bought(HttpServletRequest request, Model model){
        User user = check(request);
        if(user != null){
            List<GoodsRecord> goodsRecords = goodsRecordService.selectMyGoodsByUserId(user.getUserId());
            model.addAttribute("goodsBoughtRecordList", goodsRecords);
            return "userIndex/boughtGoods";
        }else {
            return "login";
        }

    }

    @RequestMapping("/deleteTransaction")
    @ResponseBody
    public boolean deleteTransaction(Integer id, HttpServletRequest request, Model model){
        User user = check(request);
        if (user != null){
            if (goodsRecordService.deleteBookGoodsRecordById(id) > 0){
                return true;
            }else {
                return false;
            }
        }
        return false;
    }

}
